package com.tangyuan.common.util.security;

import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/**
 * @描述: 对称密钥密码算法工具类
 * @Author: TangYuan
 * @Date: 2024/9/23 下午5:07
 */
public class CipherUtils {
    
    /**
     * 生成随机秘钥
     * @param keyBitSize    字节大小
     * @param algorithmName 算法名称
     * @return 创建密匙
     */
    public static Key generateNewKey(int keyBitSize, String algorithmName) {
        KeyGenerator kg;
        try {
            kg = KeyGenerator.getInstance(algorithmName);
        } catch (NoSuchAlgorithmException e) {
            String msg = "无法获取 " + algorithmName + " 算法。这是运行所必需的。";
            throw new IllegalStateException(msg, e);
        }
        kg.init(keyBitSize);
        return kg.generateKey();
    }
}
